/**
 * 文件：EditRadioBox.java
 * 包名：com.ronri.list.editview.controls;   --- 正式发布时需要修正
 * 说明：单选择Box
 * 作者：MS During
 * 日期：2017/5/4
 * 历史：
 */

package com.ronri.list.editview.controls;

import android.content.Intent;
import android.view.View;
import android.widget.PopupWindow;

import com.cz.nongyetongv5.R;
import com.ronri.ams.sample.activity_sample_list;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


public class EditRadioBox extends BaseControl {

    // 弹框
    PopupWindow popupWindow = null;

    // 列表结果
    private ArrayList<SelectItem> mItems = new ArrayList<SelectItem>();

    // 默认当前选择
    private int mSelectIndex = -1;

    /***
     * 列表内容
     */
    public class SelectItem{
        public String Name = "";
        public int Id = -1;
        public int Orr = 0;

        public SelectItem() {

        }

        public SelectItem(String name, int id, int orr) {
            Name = name;
            Id = id;
            Orr = orr;
        }

        @Override
        public String toString() {
            return Name;
        }
    }

    /***
     * 构造函数
     */
    public EditRadioBox() {
        super();
        this.ControlName = "EditRadioBox";
        this.BindLayout(R.layout.contorl_default_select);
    }

    /***
     * 构造函数
     * @param arrayItems
     * @param nSelectItem
     */
    public EditRadioBox(String arrayItems, int nSelectItem) {
        super();

        this.ControlName = "EditRadioBox";
        this.BindLayout(R.layout.contorl_default_select);

        if(mItems == null)
            mItems = new ArrayList<SelectItem>();

        try{
            JSONArray jarray = new JSONArray(arrayItems);

            for(int i = 0; i < jarray.length(); i++){
                JSONObject jobject = jarray.getJSONObject(i);
                mItems.add(new SelectItem(jobject.getString("name"), jobject.getInt("id"), jobject.getInt("orr")));

                if(nSelectItem == jobject.getInt("id")){
                    mSelectIndex = nSelectItem;
                }
            }

        }catch (JSONException ex)
        {

        }
    }

    /***
     * 显示控件
     * @return
     */
    @Override
    public boolean ShowEditControl(View Anchor) {
        Intent intent = new Intent(getContext(), activity_sample_list.class);
        getContext().startActivity(intent);
//
//        WindowManager manager = (WindowManager) SystemApplication.getInstance().getSystemService(Context.WINDOW_SERVICE);
//        @SuppressWarnings("deprecation")
//        int nWidth = manager.getDefaultDisplay().getWidth();
//        int nHeight = manager.getDefaultDisplay().getHeight();
//
//        View view = getView();
//
//        ImageView imgViewCancel = (ImageView) view.findViewById(R.id.edit_fail);
//        TextView imgViewOK = (TextView)view.findViewById(R.id.edit_success);
//        ListView listView = (ListView)view.findViewById(R.id.listControl);
//        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//        ListAdapter adapter = new ArrayAdapter<SelectItem>(getContext(), R.layout.item_single_choice, mItems) {
//            @Override
//            public View getView(int position, View convertView, ViewGroup parent) {
//                final ChoiceView view;
//                if(convertView == null) {
//                    view = new ChoiceView(getContext());
//                } else {
//                    view = (ChoiceView)convertView;
//                }
//                view.setText(getItem(position).toString());
//                return view;
//            }
//        };
//        listView.setAdapter(adapter);
//
//        /***
//         * 放弃选择
//         */
//        imgViewCancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                popupWindow.dismiss();
//            }
//        });
//
//
//        /***
//         * 确认返回结果集
//         */
//        imgViewOK.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//                if(BaseControlEevnt != null){
//
//                    // 校验数据
//                    boolean bRet = Validate();
//
//                    // 回调，传递结果
//                    BaseControlEevnt.ReturnValue(this);
//                }
//
//                popupWindow.dismiss();
//            }
//        });
//
//
//        popupWindow = new PopupWindow(view,
//                WindowManager.LayoutParams.MATCH_PARENT,
//                nHeight - SystemManager.getStatusBarHeight(), true);
//
//        listView.setOnKeyListener(new View.OnKeyListener() {
//            @Override
//            public boolean onKey(View v, int keyCode, KeyEvent event) {
//
//                if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK)
//                    popupWindow.dismiss();
//
//                return false;
//            }
//        });
//
//        // 设置弹出窗体显示时的动画，从底部向上弹出
//        popupWindow.setAnimationStyle(R.style.take_photo_anim);
//        popupWindow.showAtLocation(Anchor, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

        return true;
    }

    @Override
    public boolean HideEditControl() {
        return false;
    }

    @Override
    public boolean Validate() {
        return false;
    }
}
